package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.UserAddress;
import com.ddyx.entity.WxUser;
import com.ddyx.mapper.UserAddressMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.service.UserAddressService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class UserAddressServiceImpl implements UserAddressService {
	
	
	@Resource
	UserAddressMapper userAddressMapper;
	
	@Resource
	WxUserMapper  wxUserMapper;
	
	@Override
	public ResponseForm getUserAddress(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			if(!StringUtils.isNotBlank(wxUid)){
				result.setStatus(false);
				result.setMessage("参数错误");
				log.error("参数错误");
				return result;
			}
			WxUser user = wxUserMapper.selectByPrimaryKey(wxUid);
			if(user==null){
				result.setStatus(false);
				result.setMessage("用户不存在");
				log.error("用户不存在");
				return result;
			}
			Example example  = new Example(UserAddress.class);
			example.setOrderByClause("default_options DESC");
			Criteria criteria=example.createCriteria();
			criteria.andEqualTo("wxUid", wxUid);
			criteria.andEqualTo("status", 0);
			List<UserAddress> list =userAddressMapper.selectByExample(example);
		   
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public ResponseForm addUserAddress(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			if(!StringUtils.isNotBlank(wxUid)){
				result.setStatus(false);
				result.setMessage("参数错误");
				log.error("参数错误");
				return result;
			}
			WxUser user = wxUserMapper.selectByPrimaryKey(wxUid);
			if(user==null){
				result.setStatus(false);
				result.setMessage("用户不存在");
				log.error("用户不存在");
				return result;
			}
			UserAddress userAddress = new UserAddress();
			MyBeanUtils.populate(userAddress, paramMap);
		
			userAddress.setCreateTime(new Date());
			userAddressMapper.insertSelective(userAddress);
			
//			User user = new User();
//			MyBeanUtils.populate(user, paramMap);
//			//user.setName(String.valueOf(paramMap.get("name")));
//			userMapper.updateByPrimaryKeySelective(user);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public ResponseForm updateUserAddress(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String wxUid = (String) paramMap.get("wxUid");
			if(!StringUtils.isNotBlank(wxUid)){
				result.setStatus(false);
				result.setMessage("参数错误");
				log.error("参数错误");
				return result;
			}
			WxUser user = wxUserMapper.selectByPrimaryKey(wxUid);
			if(user==null){
				result.setStatus(false);
				result.setMessage("用户不存在");
				log.error("用户不存在");
				return result;
			}
			
			UserAddress userAddress = new UserAddress();
			MyBeanUtils.populate(userAddress, paramMap);
			if(userAddress.getDefaultOptions()!=null && userAddress.getDefaultOptions()==1){
				userAddressMapper.updateDefaultOptions(wxUid);
			}
			userAddressMapper.updateByPrimaryKeySelective(userAddress);
			//userAddressMapper.updateByExampleSelective(record, example)
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}


	@Override
	public ResponseForm getUserAddressById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String addressId = (String) paramMap.get("id");
			if(!StringUtils.isNotBlank(addressId)){
				result.setStatus(false);
				result.setMessage("参数错误");
				log.error("参数错误");
				return result;
			}
			UserAddress userAddress = new UserAddress();
			MyBeanUtils.populate(userAddress, paramMap);
			userAddress=userAddressMapper.selectOne(userAddress);
			result.setData(userAddress);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作错误");
			log.error("操作错误");
		}
		return result;
	}


}
